home *** CD-ROM | disk | FTP | other *** search
/ Dynamic HTML Construction Kit / Dynamic HTML Construction Kit.iso / earthlink / nscomm / java40.jar / java / net / InetAddress.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-11-03  |  5.2 KB  |  272 lines

  1. package java.net;
  2.  
  3. import java.io.Serializable;
  4. import java.util.Hashtable;
  5.  
  6. public final class InetAddress implements Serializable {
  7.    String hostName;
  8.    int address;
  9.    int family;
  10.    private static final long serialVersionUID = 3286316764910316507L;
  11.    static Hashtable addressCache;
  12.    static InetAddress unknownAddress;
  13.    static InetAddress anyLocalAddress;
  14.    static InetAddress localHost;
  15.    static InetAddress[] unknown_array;
  16.    static InetAddressImpl impl;
  17.    private static InetAddress loopbackHost;
  18.  
  19.    InetAddress() {
  20.       this.family = impl.getInetFamily();
  21.    }
  22.  
  23.    public InetAddress(int var1) {
  24.       this.address = var1;
  25.    }
  26.  
  27.    InetAddress(String var1, byte[] var2) {
  28.       this.hostName = new String(var1);
  29.       this.family = impl.getInetFamily();
  30.       this.address = var2[3] & 255;
  31.       this.address |= var2[2] << 8 & '\uff00';
  32.       this.address |= var2[1] << 16 & 16711680;
  33.       this.address |= var2[0] << 24 & -16777216;
  34.    }
  35.  
  36.    public boolean isMulticastAddress() {
  37.       return (this.address & -268435456) == -536870912;
  38.    }
  39.  
  40.    public String getHostName() {
  41.       if (this.hostName == null) {
  42.          try {
  43.             this.hostName = new String(impl.getHostByAddr(this.address));
  44.             InetAddress[] var1 = (InetAddress[])addressCache.get(this.hostName);
  45.             if (var1 != null) {
  46.                for(int var2 = 0; var2 < var1.length; ++var2) {
  47.                   if (this.hostName.equalsIgnoreCase(var1[var2].hostName) && this.address != var1[var2].address) {
  48.                      this.hostName = this.getHostAddress();
  49.                      break;
  50.                   }
  51.                }
  52.             } else {
  53.                var1 = new InetAddress[]{this};
  54.                addressCache.put(this.hostName, var1);
  55.             }
  56.  
  57.             SecurityManager var6 = System.getSecurityManager();
  58.             if (var6 != null) {
  59.                var6.checkConnect(this.hostName, -1);
  60.             }
  61.          } catch (SecurityException var3) {
  62.             this.hostName = this.getHostAddress();
  63.          } catch (UnknownHostException var4) {
  64.             this.hostName = this.getHostAddress();
  65.          }
  66.       }
  67.  
  68.       return this.hostName;
  69.    }
  70.  
  71.    public byte[] getAddress() {
  72.       byte[] var1 = new byte[]{(byte)(this.address >>> 24 & 255), (byte)(this.address >>> 16 & 255), (byte)(this.address >>> 8 & 255), (byte)(this.address & 255)};
  73.       return var1;
  74.    }
  75.  
  76.    public String getHostAddress() {
  77.       return (this.address >>> 24 & 255) + "." + (this.address >>> 16 & 255) + "." + (this.address >>> 8 & 255) + "." + (this.address & 255);
  78.    }
  79.  
  80.    public int hashCode() {
  81.       return this.address;
  82.    }
  83.  
  84.    public boolean equals(Object var1) {
  85.       return var1 != null && var1 instanceof InetAddress && ((InetAddress)var1).address == this.address;
  86.    }
  87.  
  88.    public String toString() {
  89.       return this.getHostName() + "/" + this.getHostAddress();
  90.    }
  91.  
  92.    public static InetAddress getByName(String var0) throws UnknownHostException {
  93.       if (var0 != null && var0.length() != 0) {
  94.          if (!Character.isDigit(var0.charAt(0))) {
  95.             return getAllByName0(var0)[0];
  96.          } else {
  97.             int var1 = 0;
  98.             int var2 = 0;
  99.             char[] var3 = var0.toCharArray();
  100.             int var4 = 0;
  101.  
  102.             label53:
  103.             while(var4 < var3.length) {
  104.                char var5 = var3[var4];
  105.                if (var5 >= '0' && var5 <= '9') {
  106.                   int var6 = 0;
  107.  
  108.                   while(true) {
  109.                      if (var5 != '.') {
  110.                         if (var5 < '0' || var5 > '9') {
  111.                            return getAllByName0(var0)[0];
  112.                         }
  113.  
  114.                         var6 = var6 * 10 + var5 - 48;
  115.                         ++var4;
  116.                         if (var4 < var3.length) {
  117.                            var5 = var3[var4];
  118.                            continue;
  119.                         }
  120.                      }
  121.  
  122.                      if (var6 > 255) {
  123.                         return getAllByName0(var0)[0];
  124.                      }
  125.  
  126.                      var1 = (var1 << 8) + var6;
  127.                      ++var2;
  128.                      ++var4;
  129.                      continue label53;
  130.                   }
  131.                }
  132.  
  133.                return getAllByName0(var0)[0];
  134.             }
  135.  
  136.             if (var2 == 4 && !var0.endsWith(".")) {
  137.                InetAddress var7 = new InetAddress();
  138.                var7.address = var1;
  139.                var7.hostName = null;
  140.                return var7;
  141.             } else {
  142.                return getAllByName0(var0)[0];
  143.             }
  144.          }
  145.       } else {
  146.          return loopbackHost;
  147.       }
  148.    }
  149.  
  150.    public static InetAddress[] getAllByName(String var0) throws UnknownHostException {
  151.       if (var0 != null && var0.length() != 0) {
  152.          if (Character.isDigit(var0.charAt(0))) {
  153.             InetAddress[] var1 = new InetAddress[]{getByName(var0)};
  154.             return var1;
  155.          } else {
  156.             return getAllByName0(var0);
  157.          }
  158.       } else {
  159.          throw new UnknownHostException("empty string");
  160.       }
  161.    }
  162.  
  163.    private static InetAddress[] getAllByName0(String var0) throws UnknownHostException {
  164.       Object var1 = null;
  165.       Object var2 = null;
  166.       SecurityManager var3 = System.getSecurityManager();
  167.       if (var3 != null) {
  168.          var3.checkConnect(var0, -1);
  169.       }
  170.  
  171.       Hashtable var4 = addressCache;
  172.       synchronized(var4){}
  173.  
  174.       try {
  175.          var1 = addressCache.get(var0);
  176.          if (var1 == null) {
  177.             try {
  178.                byte[][] var6 = impl.lookupAllHostAddr(var0);
  179.                InetAddress[] var7 = new InetAddress[var6.length];
  180.  
  181.                for(int var8 = 0; var8 < var6.length; ++var8) {
  182.                   byte[] var9 = var6[var8];
  183.                   var7[var8] = new InetAddress(var0, var9);
  184.                }
  185.  
  186.                var1 = var7;
  187.             } catch (UnknownHostException var14) {
  188.                var1 = unknown_array;
  189.             }
  190.  
  191.             addressCache.put(var0, var1);
  192.          }
  193.       } catch (Throwable var15) {
  194.          throw var15;
  195.       }
  196.  
  197.       if (var1 == unknown_array) {
  198.          throw new UnknownHostException(var0);
  199.       } else {
  200.          try {
  201.             var2 = ((InetAddress[])var1).clone();
  202.             if (var2 == null) {
  203.                throw new CloneNotSupportedException();
  204.             }
  205.          } catch (CloneNotSupportedException var13) {
  206.             ((Throwable)var13).printStackTrace();
  207.          }
  208.  
  209.          return (InetAddress[])var2;
  210.       }
  211.    }
  212.  
  213.    public static InetAddress getLocalHost() throws UnknownHostException {
  214.       if (localHost.equals(unknownAddress)) {
  215.          throw new UnknownHostException();
  216.       } else {
  217.          if (localHost.address == -1) {
  218.             SecurityManager.enablePrivilege("UniversalConnect");
  219.             localHost = getAllByName(localHost.hostName)[0];
  220.          }
  221.  
  222.          return localHost;
  223.       }
  224.    }
  225.  
  226.    static {
  227.       SecurityManager.enablePrivilege("UniversalLinkAccess");
  228.       System.loadLibrary("net");
  229.       addressCache = new Hashtable();
  230.       SecurityManager.enablePrivilege("UniversalPropertyRead");
  231.       String var0 = System.getProperty("impl.prefix", "");
  232.  
  233.       try {
  234.          impl = null;
  235.          impl = (InetAddressImpl)Class.forName("java.net." + var0 + "InetAddressImpl").newInstance();
  236.       } catch (ClassNotFoundException var5) {
  237.          System.err.println("Class not found: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  238.       } catch (InstantiationException var6) {
  239.          System.err.println("Could not instantiate: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  240.       } catch (IllegalAccessException var7) {
  241.          System.err.println("Cannot access class: java.net." + var0 + "InetAddressImpl:\ncheck impl.prefix property " + "in your properties file.");
  242.       }
  243.  
  244.       if (impl == null) {
  245.          try {
  246.             impl = (InetAddressImpl)Class.forName("java.net.InetAddressImpl").newInstance();
  247.          } catch (Exception var4) {
  248.             throw new Error("System property impl.prefix incorrect");
  249.          }
  250.       }
  251.  
  252.       unknownAddress = new InetAddress();
  253.       anyLocalAddress = new InetAddress();
  254.       impl.makeAnyLocalAddress(anyLocalAddress);
  255.       byte[] var1 = new byte[]{127, 0, 0, 1};
  256.       loopbackHost = new InetAddress("localhost", var1);
  257.  
  258.       try {
  259.          localHost = new InetAddress();
  260.          localHost.hostName = impl.getLocalHostName();
  261.          localHost.address = -1;
  262.       } catch (Exception var3) {
  263.          localHost = unknownAddress;
  264.       }
  265.  
  266.       String var2 = new String("0.0.0.0");
  267.       unknown_array = new InetAddress[1];
  268.       unknown_array[0] = new InetAddress(var2, unknownAddress.getAddress());
  269.       addressCache.put(var2, unknown_array);
  270.    }
  271. }
  272.